cd "~/Dropbox/research/Denmark Housing Populism/replicationfiles/"
*REPLICATION FILES
*Article: Sheltering Populists?House Prices and the Support for Populist Parties
*Authors: Ben Ansell, Frederik Hjorth, Jacob Nyrup & Martin Vinæs Larsen
*Software: STATA 16.0
*Dependencies: blindschemes

*Replicated in this file: Figure 5, 6; Tables D3, D4, D5, M1 N1. N2, N3.


*logfile
log using "log_nordic.txt", replace t

****************************************************************

**Figures and Tables for the Main Text.

*Figure 4
use "data_no_ziplvl.dta", clear //Norway's part
scatter pshare hpnew, by(year, note("") legend(off)  col(4))  msym(O) mcolor(black*0.7%10) || ///
lfit pshare hpnew, scheme(plotplain) lcolor(black) lpattern(solid) ///
|| lowess pshare hpnew, lpattern(dash) ytitle("Support for the Progress Party") lcolor(black) xtitle("Price Per Square Meter (EUR)") ///
xsize(10) xlabel(0 "0" 2.5 "25" 5 "50" 7.5 "75" 10 "10,000") ylabel(0(10)50) name(no_overview, replace) nodraw
use "data_se_ziplvl.dta", clear //Sweden's part
scatter sdshare hplevel_maklar, by(year, note("") legend(off)  col(4))  msym(O) mcolor(black*0.7%10) || ///
lfit sdshare hplevel_maklar, scheme(plotplain) lcolor(black) lpattern(solid) ///
|| lowess sdshare hplevel_maklar, lpattern(dash) ytitle("Support for the Sweden Democrats") lcolor(black) xtitle("Price Per Square Meter (EUR)") ///
xsize(10) xlabel(0 "0"  2 "2" 4 "4"  6 "6" 8 "8,000") ylabel(0(10)50)  name(se_overview, replace) nodraw
use "data_fi_ziplvl.dta", clear //Finland's part, linear fits exclude Swedish majority party
scatter psshare hplevel_nominal  if   finnishless50==1, by(year, note("") legend(off)  col(4)) msym(Dh) mcolor(black)  mcolor(%80)  || ///
scatter psshare hplevel_nominal  if   finnishless50!=1,  msym(O) mcolor(black*0.7%10) || ///
lfit psshare hplevel_nominal  if finnishless50!=1, scheme(plotplain) lcolor(black) lpattern(solid) ///
|| lowess psshare hplevel_nominal if finnishless50!=1, lpattern(dash) ytitle("Support for the True Finns") lcolor(black) xtitle("Price Per Square Meter (EUR)") ///
xsize(10) xlabel(0 "0" 1 "1" 2 "2" 3 "3" 4 "4,000") ylabel(0(10)50) name(fi_overview, replace) nodraw
*constructing graph
graph combine se_overview no_overview fi_overview, scheme(plotplain) col(1) ysize(8)
graph export "figure5.pdf", replace 



****************************************************************

**This next bit of code creates both tables for Appendix N, which present the full results for Sweden, Norway and Finland, as well as Figure 6 in the main text. 

*first we need some tempfiles to store estimates
tempfile dk1 dk2 dk3 dk4 dk5 no1 no2 no3 no4 no5 se1 se2 se3 se4 se5 fi1 fi2 fi3 fi4 fi5


*Sweden tables and estimates for Figure 6
use "data_se_ziplvl.dta", clear
eststo e: xtreg sddif hpdif difuppersecond difpostsecond difpostgrad difemploymentrate difgrossmedianincome difp_foreign diflogpopdens difpmulti i.year, re vce(cluster id)
margins, dydx(hpdif) saving(se5, replace)
eststo a: xtreg sddif hpdif i.year if e(sample)==1, re vce(cluster id)
margins, dydx(hpdif) saving(se1, replace)
eststo b: xtreg sddif hpdif diflogpopdens difpmulti i.year if e(sample)==1, re vce(cluster id)
margins, dydx(hpdif) saving(se2, replace)
eststo c: xtreg sddif hpdif c.p_foreign logpopdens pmulti i.year if e(sample)==1, re vce(cluster id)
margins, dydx(hpdif) saving(se3, replace)
eststo d: xtreg sddif hpdif difemploymentrate difgrossmedianincome difp_foreign diflogpopdens difpmulti i.year  if e(sample)==1, re vce(cluster id)
margins, dydx(hpdif) saving(se4, replace)
foreach x in a b c d e{
estimates restore `x'
estadd local yearfe "$\checkmark$"
}
esttab a b c d e using tablen1.tex, keep(hpdif difuppersecond difpostsecond difpostgrad difemploymentrate difgrossmedianincome difp_foreign diflogpopdens difpmulti ) replace ///
nostar se nomtitles b(%9.1f) nogap  ///
label stats(yearfe N rmse, fmt(%8.0f %8.0f %8.3f)   label("Year FE" "Observations" "RMSE")) nonotes addnotes("Clustered standard errors in parentheses.") ///
title(Support for the Sweden Democrats and Housing Prices} \footnotesize \label{sefd)

*Norway tables and estimates for Figure 6
use "data_no_ziplvl.dta", clear
eststo e: xtreg difpshare difhpnew difpuppersecond difptertiaryshort difptertiarylong difunemployment difincome difpnonwestern diflogpopdens difpone difpmulti i.year , fe vce(cluster id)
margins, dydx(difhp) saving(no5, replace)
eststo a: xtreg difpshare difhpnew i.year  if e(sample)==1,  vce(cluster id)
margins, dydx(difhp) saving(no1, replace)
eststo b: xtreg difpshare difhpnew diflogpopdens difpone difpmulti i.year  if e(sample)==1,  vce(cluster id)
margins, dydx(difhp) saving(no2, replace)
eststo c: xtreg difpshare difhpnew difpnonwestern diflogpopdens difpone difpmulti i.year if e(sample)==1,  vce(cluster id)
margins, dydx(difhp) saving(no3, replace)
eststo d: xtreg difpshare difhpnew difunemployment difincome difpnonwestern diflogpopdens difpone difpmulti i.year  if e(sample)==1, vce(cluster id)
foreach x in a b c d e{
estimates restore `x'
estadd local yearfe "$\checkmark$"
}
margins, dydx(difhp) saving(no4, replace)
esttab a b c d e using tablen2.tex, keep(difhpnew difpuppersecond difptertiaryshort difptertiarylong difunemployment difincome difpnonwestern diflogpopdens difpone difpmulti) replace ///
nostar se nomtitles b(%9.1f) nogap ///
label stats(yearfe N rmse, fmt(%8.0f %8.0f %8.3f)   label("Year FE" "Observations" "RMSE"))  nonotes addnotes("Clustered standard errors in parentheses.") ///
title(Support for the Progress Party  and Housing Prices} \footnotesize \label{nofd)

*Finland tables and estimates for Figure 6
use "data_fi_ziplvl.dta", clear
preserve
drop if finnishless50 == 1 //dropping majority Swedish municipalities
ta finnish
eststo e: xtreg difpsshare difhplevel_nominal difpuppersecondary difpshortcycle difpbachelor difplong difunemployment difgrossincomeperunit difpnonwestern diflogpopdens difpone difpmulti i.year, re vce(cluster id)
margins, dydx(difhp) saving(fi5, replace)
eststo a: xtreg difpsshare difhplevel_nominal i.year if e(sample)==1, re vce(cluster id)
margins, dydx(difhp) saving(fi1, replace)
eststo b: xtreg difpsshare difhplevel_nominal diflogpopdens difpone difpmulti i.year if e(sample)==1, re vce(cluster id)
margins, dydx(difhp) saving(fi2, replace)
eststo c: xtreg difpsshare difhplevel_nominal difpnonwestern diflogpopdens difpone difpmulti i.year if e(sample)==1, re vce(cluster id)
margins, dydx(difhp) saving(fi3, replace)
eststo d: xtreg difpsshare difhplevel_nominal difunemployment difgrossincomeperunit difpnonwestern diflogpopdens difpone difpmulti i.year if e(sample)==1, re vce(cluster id)
margins, dydx(difhp) saving(fi4, replace)
foreach x in a b c d e{
estimates restore `x'
estadd local yearfe "$\checkmark$"
}
esttab a b c d e using tablen3.tex, keep(difhplevel_nominal difpuppersecondary difpshortcycle difpbachelor difplong difunemployment difgrossincomeperunit difpnonwestern diflogpopdens difpone difpmulti) replace ///
nostar se nomtitles b(%9.1f) nogap ///
label  stats(yearfe N rmse, fmt(%8.0f %8.0f %8.3f)   label("Year FE" "Observations" "RMSE")) nonotes addnotes("Clustered standard errors in parentheses.")  ///
title(Support for the True Finns and Housing Prices} \footnotesize \label{fifd)
restore

*Danish estimates for Figure 6.
use "data_dk_ziplvl.dta"
*rescaling house price variable to be in 1000EUR
replace hpdif=(hpdif*10)/7.5
*saving models for comparative analyses
xtreg dfdif hpdif i.year , re vce(cluster zip)
margins, dydx(hpdif) saving(dk1)
xtreg dfdif hpdif difpopdens difsolo diftenppl i.year , re vce(cluster zip)
margins, dydx(hpdif) saving(dk2)
xtreg dfdif hpdif difpopdens difsolo diftenppl difnwimm i.year , re vce(cluster zip)
margins, dydx(hpdif) saving(dk3)
xtreg dfdif hpdif difpopdens difunem difmedian difsolo diftenppl difnwimm i.year  , re vce(cluster zip)
margins, dydx(hpdif) saving(dk4)
xtreg dfdif hpdif diflow_service diflow_manuf difhighlvl difpopdens difunem difmedian difsolo diftenppl difnwimm i.year  , re vce(cluster zip)
margins, dydx(hpdif) saving(dk5)


*Combining estimates for Figure 6.
use dk1, clear
foreach x in 2 3 4 5{
append using dk`x'
}
foreach z in se no fi {
append using  `z'1
foreach x in 2 3 4 5{
append using `z'`x'
}
}
 gen id=_n
replace id =id+2 if id >5
replace id =id+2 if id >12
replace id =id+2 if id >19
gen category=id
replace cat=id-7 if id >6
replace cat=id-14 if id >13
replace cat=id-21 if id >19
twoway rspike _ci_l _ci_u i , lcolor(black*0.5%50) || /// drawing Figure 6
scatter _margin i if cat==1, msym(Oh) mcolor(black) || ///
scatter _margin i if cat==2, msym(O) mcolor(black) || ///
scatter _margin i if cat==3, msym(Dh) mcolor(black) || ///
scatter _margin i if cat==4, msym(D) mcolor(black) || ///
scatter _margin i if cat==5, msym(T) mcolor(black) ///
xline(6.7 13.5 20.5) scheme(plotplain) yline(0, lpattern(solid)) ///
xlabel(0 " " 3.25 "Danish People's Party" 10.25 "Sweden Democrats"  17.25 "Progress Party (NO)"  24.25 "True Finns" 27 " ", labsize(medium) notick nogrid ) xtitle(" ") xsize(7) ///
text(-2.9 1 "No" "Controls", size(small)) text(-1.7 2 "Urban", size(small))  legend(off) ///
text(-2.8 3 "Immig.", size(small)) ylabel(-5(1)1) text(-1.6 4 "Econ.", size(small)) ////
 text(-2.9 5 "Labor" "Market", size(small)) ytitle("Effect of Housing Prices (EUR 1,000)", size(medium)) 
 graph export "figure6.pdf", replace 
 
  *removing aux files
 foreach z in dk  no fi se {
foreach x in 1 2 3 4 5{
erase `z'`x'.dta
}
}
 
 
****************************************************************


**Figures and Tables for the Appendix.

*Descriptive Statistics (Appendix D).
use "data_fi_ziplvl.dta", clear //descriptives for Finland
la var finnishless50 "Swedish Majority"
replace finnishless50=0 if finnishless50==.
la var hplevel_fixed "Fixed Housing Prices EUR(1,000)"
file open anyname using "tabled5.txt", write text replace 
file write anyname  _newline  _col(0)  "\begin{table} [htbp] \centering \caption{Descriptive statistics, Finland\label{desdnes}} \footnotesize \begin{tabular}{l*{6}{c}}\hline\hline "
file write anyname _newline _col(0) "&Mean & SD & Min & Median & Max & n\\ \hline"
foreach x of var  psshare hplevel_nominal hplevel_fixed puppersecondary pshortcycle pbachelor plong unemployment grossincomeperunit pnonwestern logpopdens pone pmulti  {
su  `x' , d
file write anyname  _newline  _col(0) (`"`: var label `x''"') "&" _col(25) %9.2f  (r(mean)) " &" _col(45) %9.2f  (r(sd)) " &" _col(65) %9.2f  (r(min)) " &"   _col(85) %9.2f  (r(p50)) " &" _col(105) %9.2f  (r(max)) " &" _col(125) %9.0f  (r(N)) " \\"
}
file write anyname _newline _col(0) "\hline\hline"
file write anyname _newline _col(0) "\end{tabular}"
file write anyname _newline _col(0) "\end{table}"
file close anyname
use "data_se_ziplvl.dta", clear //descriptives for Sweden
la var hplevel_maklar_f "Fixed Housing Prices EUR(1,000)"
file open anyname using "tabled3.txt", write text replace 
file write anyname  _newline  _col(0)  "\begin{table} [htbp] \centering \caption{Descriptive Statistics, Sweden\label{desdnes}} \footnotesize \begin{tabular}{l*{6}{c}}\hline\hline "
file write anyname _newline _col(0) "&Mean & SD & Min & Median & Max & n\\ \hline"
foreach x of var  sdshare hplevel_maklar hplevel_maklar_f uppersecond postsecond postgrad employmentrate grossmedianincome p_foreign logpopdens pmulti {
su  `x' , d
file write anyname  _newline  _col(0) (`"`: var label `x''"') "&" _col(25) %9.2f  (r(mean)) " &" _col(45) %9.2f  (r(sd)) " &" _col(65) %9.2f  (r(min)) " &"   _col(85) %9.2f  (r(p50)) " &" _col(105) %9.2f  (r(max)) " &" _col(125) %9.0f  (r(N)) " \\"
}
use "data_no_ziplvl.dta", clear //descriptives for Norway
file write anyname _newline _col(0) "\hline\hline"
file write anyname _newline _col(0) "\end{tabular}"
file write anyname _newline _col(0) "\end{table}"
file close anyname
la var hpnew_f "Fixed Housing Prices EUR(1,000)"
file open anyname using "tabled4.txt", write text replace 
file write anyname  _newline  _col(0)  "\begin{table} [htbp] \centering \caption{Descriptive Statistics, Norway \label{desdnes}} \footnotesize \begin{tabular}{l*{6}{c}}\hline\hline "
file write anyname _newline _col(0) "&Mean & SD & Min & Median & Max & n\\ \hline"
foreach x of var  pshare hpnew hpnew_f puppersecond ptertiaryshort ptertiarylong unemployment income pnonwestern logpopdens pone pmulti {
su  `x' , d
file write anyname  _newline  _col(0) (`"`: var label `x''"') "&" _col(25) %9.2f  (r(mean)) " &" _col(45) %9.2f  (r(sd)) " &" _col(65) %9.2f  (r(min)) " &"   _col(85) %9.2f  (r(p50)) " &" _col(105) %9.2f  (r(max)) " &" _col(125) %9.0f  (r(N)) " \\"
}
file write anyname _newline _col(0) "\hline\hline"
file write anyname _newline _col(0) "\end{tabular}"
file write anyname _newline _col(0) "\end{table}"
file close anyname


*Redoing analyses with fixed prices (Appendix M).
use "data_dk_ziplvl.dta", clear
tsset district elyear
replace hpdif=hplevel_f-l.hplevel_f
eststo a: xtreg dfdif hpdif diflow difhigh diflow_service diflow_manuf difhighlvl difpopdens difunem difmedian difsolo diftenppl difnwimm i.year , fe vce(cluster zip)
use "data_se_ziplvl.dta", clear
tsset id elyear
replace hpdif=hplevel_maklar_f-l.hplevel_maklar_f
eststo b: xtreg sddif hpdif  difuppersecond difpostsecond difpostgrad difemploymentrate difgrossmedianincome difp_foreign diflogpopdens difpmulti i.year, re vce(cluster id)
use "data_no_ziplvl.dta", clear
tsset id year
gen hpdif=hpnew_f-l4.hpnew_f
eststo c: xtreg difpshare hpdif difpuppersecond difptertiaryshort difptertiarylong difunemployment difincome difpnonwestern diflogpopdens difpone difpmulti i.year , fe vce(cluster id)
use "data_fi_ziplvl.dta", clear
tsset id year
gen hpdif=hplevel_f-l4.hplevel_f
drop if finnishless50 == 1
eststo d: xtreg difpsshare hpdif difpuppersecondary difpshortcycle difpbachelor difplong difunemployment difgrossincomeperunit difpnonwestern diflogpopdens difpone difpmulti i.year , re vce(cluster id) 
foreach x in a b c d{
estimates restore `x'
estadd local yearfe "$\checkmark$"
}
la var hpdif "Housing Prices (1,000 EUR)"
esttab a b c d using "tablem1.tex", replace keep(hpdif) ///
nostar se b(%9.1f) nonum nogap  ///
label stats(yearfe yearfe N rmse, fmt(%8.0f %8.0f %8.0f %8.3f)   label("Zip code controls" "Year FE" "Observations" "RMSE"))nonotes addnotes("Clustered standard errors in parentheses.") ///
title(Support for Populist Parties and Fixed Housing Prices} \footnotesize \label{fixedprices) mtitles("DK" "SE" "NO" "FI")

*closing log
log close 
